package qf.common.security.entitiy;

import com.alibaba.fastjson2.annotation.JSONField;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class SecuriyUserDetailVO implements UserDetails {

    private SysUser user;

    private List<String> permission;

    public SecuriyUserDetailVO() {
    }

    public SecuriyUserDetailVO(SysUser user, List<String> permission) {
        this.user = user;
        this.permission=permission;
    }

    @JSONField(serialize = false)
    private List<GrantedAuthority>  authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        authorities=new ArrayList<>();
        if (permission == null || permission.size() == 0) {
            return authorities;
        }
        for (String perm : permission) {
            authorities.add(new SimpleGrantedAuthority(perm));
        }
        return authorities; //该用户有哪些权限
    }

    @Override
    public String getPassword() {
        return this.user.getPassword();
    }

    @Override
    public String getUsername() {
        return this.user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {//帐号是不是没有过期
        return !SysUser.STAT_DELETE.equals(this.user.getEnabled());
    }

    @Override
    public boolean isAccountNonLocked() { //是不是没有被锁定
        return SysUser.STAT_ENABLED.equals(this.user.getEnabled());
    }

    @Override
    public boolean isCredentialsNonExpired() {
        //凭证是不是没有过期，不在这里判断
        return true;
    }

    @Override
    public boolean isEnabled() {  //是否可用
        return SysUser.STAT_ENABLED.equals(this.user.getEnabled());
    }

    public SysUser getUser() {
        return user;
    }

    public void setUser(SysUser user) {
        this.user = user;
    }

    public List<String> getPermission() {
        return permission;
    }

    public void setPermission(List<String> permission) {
        this.permission = permission;
    }

    public void setAuthorities(List<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }
}

